import { cityList } from '@/api/task';
import { defineStore } from 'pinia';

export const taskStore = defineStore({
  id: 'task',
  state: () => ({
    cityValue: localStorage.getItem('city') || '北京',
    cityList: [],
    areaList: [],
  }),
  actions: {
    async getCityList() {
      const res: any = await cityList({});
      if (res) {
        this.cityList = res;
        let city: any = [];
        for (var i = 0; i < res.length; i++) {
          city.push({
            text: res[i].name,
            children: [],
          });
          if (res[i].children && res[i].children.length) {
            for (var j = 0; j < res[i].children.length; j++) {
              city[i].children.push({
                text: res[i].children[j].name,
              });
            }
          }
        }
        this.areaList = city;
      }
    },
    setCityValue(value: string) {
      this.cityValue = value;
    },
  },
});
